package work3_10;

import java.util.Scanner;

public class BinarySearch {
    public static void main(String[] args) {
        Scanner input =new Scanner(System.in);
        //定义数组，输入数字
        System.out.println("请输入要搜索的数字：");
        int num=input.nextInt();
        int[] arr={1,2,3,4,5,6,7,8,9,10};
        //定义左右指针
        int left=0;
        int rigth=arr.length-1;
        boolean flag=true;
        //while循环判断素组元素
        while(left<rigth){
            int middle=(left+rigth)/2;
            if(arr[middle]==num){
                flag=true;
                System.out.println(num+"在数组中的下标是"+middle);
                break;
            }else if(arr[middle]>num){
                rigth=middle-1;
            }else{
                left=middle+1;
            }
        }
        if(!flag){
            System.out.println("数组中没有"+num);
        }

    }

}

